<?php
namespace Admin\Controller;
class UserMessageController extends CommonController{
    public function _initialize() {
		parent::_initialize();
        $this->_mod = D('UserMessage');
    }

    public function _before_index() {
		//$this->list_relation = true;
        $this->options = $this->check();
        $p = I('p',1,'intval');
        $this->assign('p',$p);

        $this->assign('list_table',true);
    }
    public function index(){
        $page  = I('get.p',1,'int');
        $obj = D("UserMessage as ur");
        $fex = C('DB_PREFIX');
        $map = $this->_search();
        $pagesize = 20;
        $count = $obj->join('left join '.$fex.'user as u on u.id = ur.user_id')
            ->where($map)->count();
        $pageObj = new \Org\Yike\Page($count,$pagesize);
        $lists = $obj->join('left join '.$fex.'user as u on u.id = ur.user_id')
                     -> field('ur.*,u.username')
                     -> where($map)
                     -> order('ur.send_time desc')
                     -> page($page,$pageObj->listRows)
                     -> select();
        $this->assign('list',$lists);
        $this->assign('page',$pageObj->show());
        $this->display();
    }

    protected function _search() {
        $map = array();
        $type = I('get.cate_id',0,'int');
        if($type){
            $map['ur.cate_id'] = $type;
        }
       ($keyword = I('get.keyword')) && $map['u.username|u.email'] = array('like', '%'.$keyword.'%');
        $this->assign('search', array(
            'keyword' => $keyword,
        ));
        $this->assign('type',$type);
        return $map;
    }

    public function _before_insert($data){
        $data['touser'] = I('post.touser',1,'int');
        $data['user_cate'] = I('post.user_cate',1,'int');
        $ids = array();
        switch($data['touser']){
            case 1:
                $ids = $this->getUser();
                break;
            case 2:
                $ids = $this->getUser(2,$data['user_cate']);
                break;
            case 3:
                $ids = array($data['user_id']);
                break;
        }
        if($ids){
            $sendData = array();
            foreach($ids as $id){
                $sendData[] = array(
                    'user_id' => $id,
                    'cate_id' => $data['cate_id'],
                    'title' => $data['title'],
                    'send_time' => time(),
                    'send_user' => $_SESSION['username'],
                   // 'username'  => '',
                    'content'   => $data['content'],
                    'status'    => 0
                );
            }
            $this->addAll = true;
            return $sendData;
        }else{
            $this->error('请选择用户');
        }
    }

    public function _before_edit(){
        $id = I('get.id');
        $cate = $this->_mod->field('id,user_id')->where(array('id'=>$id))->find();
        $username = D("User")->where(array('id'=>$cate['user_id']))->getField('username');
        $this->assign('username',$username);
    }

    private function getUser($t=1,$cate=''){
        $user = array();
        if($t==1){
            $user = D("User")->where(array('status'=>1))->field('id')->select();
        }else{
            if($cate){
                $user = D("UserInfo")->where(array('cate_id'=>$cate))->field('user_id as id')->select();
            }
        }
        if($user){
            $idArr = array();
            foreach($user as $v){
                $idArr[] = $v['id'];
            }
            return $idArr;
        }else{
            return $user;
        }
    }
}